package com.tencent.elife.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;
import android.widget.SectionIndexer;

/**
 * 快速定位View
 * @author tongyiguo
 */
public class SideBar extends View {
    /** 索引字母表 */
    private String[] mIndexList = new String[] {
            "A", "B", "C", "D", "E", "F", "G", 
            "H", "I", "J", "K", "L", "M", "N", 
            "O", "P", "Q", "R", "S", "T", "U",
            "V", "W", "X", "Y", "Z" };
    /** SectionIndexer */
    private SectionIndexer mSectionIndexter = null;
    /** 城市列表 */
    private ListView mListView = null;
    /** 单个索引高度 */
    private int mItemHeight = 0;
    /** 索引字体大小 */
    private float mTextSize = 0;
    /** 焦点变换显示文字 */
    private static OnIndexChangeListener mOnIndexChangeListener = null;
    /** 文字颜色 */
    private int mTextColor = 0xFFA6A9AA;
    
    
    /**
     * 构造函数
     * @param context 上下文
     */
    public SideBar(Context context) {
        super(context);
        init();
    }
    
    /**
     * 构造方法
     * @param context 上下文
     * @param attrs 参数
     */
    public SideBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    
    /**
     * 构造方法
     * @param context 上下文
     * @param attrs 参数
     * @param defStyle 默认风格
     */
    public SideBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }
    
    /**
     * 初始化参数
     */
    private void init() {

    }
    
    /**
     * 设置sidebar字体颜色
     */
    public void setSideTextColor(int color) {
        mTextColor = color;
    }
    
    /**
     * 设置索引列表
     * @param indexList
     */
    public void setIndexList(String[] indexList) {
        mIndexList = indexList;
    }

    /** 
     * 设置需要快速滑动的listView
     * @param listView 需要快速滑动的listView
     */
    public void setListView(ListView listView) {
        mListView = listView;
        mSectionIndexter = (SectionIndexer) listView.getAdapter();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        int i = (int) event.getY();
        int idx = i / mItemHeight;
        if (idx >= mIndexList.length) {
            idx = mIndexList.length - 1;
        } else if (idx < 0) {
            idx = 0;
        }
        if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) {
            if (null == mSectionIndexter) {
                mSectionIndexter = (SectionIndexer) mListView.getAdapter();
                if (null == mSectionIndexter) {
                    return true;
                }
            }
            int position = mSectionIndexter.getPositionForSection(idx);
            if (position == -1) {
                return true;
            }
            mListView.setSelection(position);
            
            if (null != mOnIndexChangeListener) {
                mOnIndexChangeListener.onChange(mIndexList[idx]);
            }
        }
        return true;
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        float widthCenter = getMeasuredWidth() / 2;
        mItemHeight = getMeasuredHeight() / mIndexList.length;
        mTextSize = (float) (getMeasuredWidth() / 2.5);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(mTextColor);
        paint.setTextSize(mTextSize);
        paint.setTextAlign(Paint.Align.CENTER);
        for (int i = 0; i < mIndexList.length; i++) {
        	canvas.drawText(mIndexList[i], widthCenter, mItemHeight + (i * mItemHeight), paint);
        }
        super.onDraw(canvas);
    }
    /**
     * 获取焦点变换监听器
     * @return 焦点变换监听器
     */
    public OnIndexChangeListener getOnIndexChangeListener() {
        return mOnIndexChangeListener;
    }
    /**
     * 设置焦点变换监听器
     * @param onIndexChangeListener 焦点变换监听器
     */
    public void setOnIndexChangeListener(OnIndexChangeListener onIndexChangeListener) {
        SideBar.mOnIndexChangeListener = onIndexChangeListener;
    }

    /**
     * 焦点变换接口
     * @author tongyiguo
     */
    public interface OnIndexChangeListener {
        void onChange(String strIndex);
    }
}